﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace SQLServerDAL
{
    public class FilmsDAL : IFilmsDAL
    {
        public Boolean InsertFilm(Film film)
        {
            try
            {
                
                FilmsDataClassesDataContext filmContext = new FilmsDataClassesDataContext();
                filmContext.Films.InsertOnSubmit(film);
                filmContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteFilm(int filmId)
        {
            try
            {
                FilmsDataClassesDataContext filmContext = new FilmsDataClassesDataContext();
                Film film = (from u in filmContext.Films
                             where u.FilmId.Equals(filmId)
                             select u).First();
                filmContext.Films.DeleteOnSubmit(film);
                filmContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateFilm(int filmId, Film film)
        {

            if (filmId != film.FilmId)
            {
                return false;
            }
            FilmsDataClassesDataContext filmContext = new FilmsDataClassesDataContext();
            Film query = (from u in filmContext.Films
                          where u.FilmId.Equals(filmId)
                          select u).First();
            //query.Password = film.Password;

            filmContext.SubmitChanges();
            return true;

        }

        public Film SelectFilmById(int filmId)
        {
            FilmsDataClassesDataContext filmContext = new FilmsDataClassesDataContext();
            Film film = (from u in filmContext.Films
                         where u.FilmId.Equals(filmId)
                         select u).First();
            return film;
        }


        public List<Film> SelectAllFilm()
        {
            FilmsDataClassesDataContext filmContext = new FilmsDataClassesDataContext();
            var query = (from u in filmContext.Films
                         select u);
            List<Film> filmList = new List<Film>();

            foreach (var film in query)
            {
                filmList.Add(film);
            }
            return filmList;
        }

    }
}
